package org.emet.dao.ash;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.emet.entity.ash.StorageInfo;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.arch.ArchDataSourceBaseDao;
import org.emet.service.dto.ash.StorageInfoSearchCriteria;
import org.springframework.stereotype.Repository;

@Repository
public class StorageInfoDao extends ArchDataSourceBaseDao<StorageInfo, Long> {

	public Page<StorageInfo> search(Page<StorageInfo> page,
			StorageInfoSearchCriteria c) {
		String hql = "from StorageInfo where 1=1 ";
		Map<String, Object> params = new HashMap<String, Object>();

		if (!StringUtils.isBlank(c.getAshItem())) {
			hql += " and ashItemCode like :code";
			params.put("code", "%" + c.getAshItem() + "%");
		}
		if (!StringUtils.isBlank(c.getDeceasedName())) {
			hql += " and deceasedName like :deceasedName";
			params.put("deceasedName", c.getDeceasedName() + "%");
		}
		if (!StringUtils.isBlank(c.getDeceasedPYName())) {
			hql += " and deceasedPYName like :deceasedPYName";
			params.put("deceasedPYName", c.getDeceasedPYName() + "%");
		}
		if (!StringUtils.isBlank(c.getRelativeName())) {
			hql += " and relativeName like :relativeName";
			params.put("relativeName", c.getRelativeName() + "%");
		}
		if (!StringUtils.isBlank(c.getRelativePYName())) {
			hql += " and relativePYName like :relativePYName";
			params.put("relativePYName", c.getRelativePYName() + "%");
		}
		if (c.getStartDate() != null) {
			hql += " and startDay >= :startDay";
			params.put("startDay", c.getStartDate());
		}
		if (c.getEndDate() != null) {
			hql += " and endDay <= :endDay";
			params.put("endDay", c.getStartDate());
		}
		hql += " order by id desc";
		return findPage(page, hql, params);
	}

}
